﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilWpf
{
    /// <summary>
    /// 属性字符串
    /// </summary>
    [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property)]
    public class GridHeaderAttribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        public string Header { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public GridHeaderAttribute(string v) { Header = v; }
    }
    /// <summary>
    /// 
    /// </summary>
    public static class UtilWpfPub
    {
        /// <summary>
        /// 输出此类的所有属性名和属性对应的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<UserControlColumnNode> GetUserControlColumnNodeList<T>() where T : new()
        {
            var result = new List<UserControlColumnNode>();
            var t = new T();
            var fields = t.GetType().GetFields();
            foreach (var item in fields)
            {
                var objs = item.GetCustomAttributes(typeof(GridHeaderAttribute), true);
                if (objs.Length > 0)
                {
                    if (item.FieldType.IsValueType || item.FieldType.Name.StartsWith("String"))
                    {
                        result.Add(new UserControlColumnNode(((GridHeaderAttribute)objs[0]).Header, item.Name));
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listObj"></param>
        /// <returns></returns>
        public static ObservableCollection<ExpandoObject> GetUserControlDataGridListObj<T>(IEnumerable<T> listObj)
        {
            ObservableCollection<ExpandoObject> ListDatas = new ObservableCollection<ExpandoObject>();
            foreach (var item in listObj)
            {
                dynamic dobj = new ExpandoObject();
                var fields = item.GetType().GetFields();
                foreach (var field in fields)
                {
                    ((IDictionary<string, object>)dobj).Add(field.Name, field.GetValue(item));
                }
                ListDatas.Add(dobj);
            }
            return ListDatas;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="dictObj"></param>
        /// <returns></returns>
        public static ObservableCollection<ExpandoObject> GetUserControlDataGridKeyValue<T1, T2>(Dictionary<T1, T2> dictObj)
        {
            ObservableCollection<ExpandoObject> ListDatas = new ObservableCollection<ExpandoObject>();
            foreach (var kvp in dictObj)
            {
                dynamic dobj = new ExpandoObject();
                ((IDictionary<string, object>)dobj).Add("Key", kvp.Key);
                ((IDictionary<string, object>)dobj).Add("Value", kvp.Value);
                ListDatas.Add(dobj);
            }
            return ListDatas;
        }
    };
    /// <summary>
    /// 
    /// </summary>
    public class UserControlColumnNode
    {
        /// <summary>
        /// 
        /// </summary>
        public string Header = "";
        /// <summary>
        /// 
        /// </summary>
        public string Binding = "";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="header"></param>
        /// <param name="binding"></param>
        public UserControlColumnNode(string header, string binding)
        {
            Header = header;
            Binding = binding;
        }
    };
    /// <summary>
    /// 
    /// </summary>
    public class UserControlItemsNode
    {
        /// <summary>
        /// 
        /// </summary>
        public string Key { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        public string Value { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public UserControlItemsNode(string key, string value)
        {
            Key = key;
            Value = value;
        }
    };
}
